package com.chanyachting.emall.action.activities;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Channel;
import com.chanyachting.emall.domain.Newsactivity;
import com.chanyachting.emall.domain.Periodical;
import com.chanyachting.emall.form.NewsActivityForm;
import com.chanyachting.emall.service.ChannelService;
import com.chanyachting.emall.service.NewsActivityService;
import com.chanyachting.emall.serviceImpl.ChannelServiceImpl;
import com.chanyachting.emall.serviceImpl.NewsActivityServiceImpl;
import com.chanyachting.emall.util.PageUtil;

public class SearchActivityNewsAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//一般页面记录数
		int COMMON_PAGE_SIZE = 20;
		
		NewsActivityForm naForm = (NewsActivityForm) form;
		NewsActivityService naService=new NewsActivityServiceImpl();
		ChannelService cService = new ChannelServiceImpl();
		
		int channelId = naForm.getChannelId();
		Channel c = cService.getOneChannel(new Channel(channelId));
		String channelName = naForm.getChannelName();
		if (c == null) {
			return mapping.findForward("error");
		}
		channelName = c.getName();
		
		List<Channel> subChannelList = cService.getSubChannel(c);
		
		if (subChannelList == null || subChannelList.size() == 0) {
			return mapping.findForward("error");
		}	
		
		int currentPage = naForm.getCurrentPage();
		int pageSize = COMMON_PAGE_SIZE;
		
		Newsactivity tmpNews = new Newsactivity();
		Periodical pd = new Periodical();
		tmpNews.setTitle(naForm.getKeyword());
		
		if ((naForm.getMonth() != null && !"".equals(naForm.getMonth())) 
				|| (naForm.getTime() != null && !"".equals(naForm.getTime()))) {
			pd.setStartMonth(naForm.getTime());
			pd.setYear(naForm.getMonth());
		} else {
			pd = null;
		}
		
		tmpNews.setChannelId(channelId);
		int recordCnt = naService.getNewsCountByKeywords1(tmpNews,pd,true);
		
		int pageCount = recordCnt%pageSize == 0?recordCnt/pageSize:(recordCnt/pageSize + 1);
		
		if (currentPage < 1) {
			currentPage = 1;
		} else if (currentPage > pageCount) {
			currentPage = 1;
		}
		
		Map<String,Newsactivity> channelList = new TreeMap<String,Newsactivity>();
		List<Newsactivity> tmpNewsact = naService.getNewsByKeywords1(tmpNews, currentPage, pageSize, pd,true);
		
		for(int i=0;i<tmpNewsact.size();i++) {
			String newsTitle = tmpNewsact.get(i).getTitle();
			if (newsTitle != null && newsTitle.length() > 28) {
				newsTitle = newsTitle.substring(0,28) + "...";
			}
			tmpNewsact.get(i).setTitle(newsTitle);
			channelList.put(getSubChannelName(subChannelList,tmpNewsact.get(i).getSubChannelId()) + "&" + i, tmpNewsact.get(i));
		}
		
		naForm.setSubChannelList(subChannelList);
		naForm.setChannelList(channelList);
		naForm.setSubChannelName("");
		naForm.setChannelName(channelName);
		
		String toPageAct = PageUtil.getGotoAction(request.getRequestURL().toString(), request.getParameterMap());
		if (toPageAct.indexOf("?") == -1) {
			toPageAct = toPageAct.replace(".do", ".do?");
		}
		
		String linkStr = PageUtil.getFy3(pageCount, currentPage, toPageAct);
		String selectionStr = PageUtil.getFy4(pageCount, currentPage, toPageAct);
		naForm.setLinkStr(linkStr);
		naForm.setSelectionStr(selectionStr);
		String actionName = toPageAct;
		if (actionName.indexOf("?") != -1) {
			actionName = actionName.substring(0,actionName.indexOf("?"));
		} else {
			actionName = actionName.substring(0);
		}
		naForm.setActionName(actionName);
		
		return mapping.findForward("searchNewsActivity");
	}
	
	private String getSubChannelName(List<Channel> cList,Integer subChannelId) {
		String tmpStr = "";
		for (Channel c:cList) {
			if (subChannelId.equals(c.getId())) {
				tmpStr = c.getName();
				break;
			}
		}
		return tmpStr;
	}
}
